import { stringToDate, sizeFormat } from "@/generic/Utils";
import { Icon, Input, Button, Checkbox } from 'antd';
import { mySelect, BeforeInputAfter } from "../components/CustomHTML";

const { TextArea } = Input;

// #### TABLE_COLS ####
export const TABLE_COLS = [
    {
        dataIndex: 'key', text: "序号", width: '', isHidden: true,
    },
    {
        dataIndex: 'desc', text: "盘名", width: '',
        canClick: { type: 'manager/updateCurrentPage', payload: { mark: 'detail' } },
    },
    {
        dataIndex: 'enable', text: "启用", width: '', isHidden: true,
        render: (isEnable, _) => isEnable
            ? <Icon type="check-circle" style={{ color: '#52c41a' }} />
            : <Icon type="close-circle" style={{ color: 'gray' }} />,
    },
    {
        dataIndex: 'permOption', text: "组权限", width: '', isHidden: true,
        render: (setModalVisible, setPermObject) => {
            return (
                <div onClick={() => {setPermObject('group'); setModalVisible(true)}}>
                    <span>group</span>&nbsp;&nbsp;
                    <Icon type="safety-certificate" theme="twoTone" />
                </div>
            )
        },
        hasModal: true,
    },
    {
        dataIndex: 'permOption', text: "用户权限", width: '', isHidden: true,
        render: (setModalVisible, setPermObject) => {
            return (
                <div onClick={() => { setPermObject('user'); setModalVisible(true) }}>
                    <span>user</span>&nbsp;&nbsp;
                    <Icon type="safety-certificate" theme="twoTone" />
                </div>
            )
        },
        hasModal: true,
    },
    {
        dataIndex: 'createdAt', text: "创建时间", width: '', isHidden: true,
        render: (date) => stringToDate(date)
    },
]

// #### HEADER_OPTS ####
export const HEADER_OPTS = {
    left: [
        {
            isGroup: false,
            text: '添加',
            className: 'headerBtns',
            args: { icon: 'plus', type: 'primary', },
            action: { type: 'manager/updateModalVisible', payload: true },
        },
        {
            isGroup: false,
            text: '删除',
            className: 'headerBtns',
            args: { icon: 'delete', type: 'danger', },
            isHidden: true,
            confirm: {
                name: '组',
                action: { type: 'manager/methodAdapter', method: 'delete', payload: {} },
            },
            msgTip: '删除',
        },
        {
            isGroup: true, className: 'headerBtns', isHidden: true,
            groups: [
                {
                    text: '启用',
                    args: { icon: 'check', },
                    action: { type: 'manager/methodAdapter', method: 'update', payload: { enable: true } },
                    msgTip: '启用',
                },
                {
                    text: '禁用',
                    args: { icon: 'close', },
                    action: { type: 'manager/methodAdapter', method: 'update', payload: { enable: false } },
                    msgTip: '禁用',
                },
            ]
        },
    ],
    right: {
        onSearch: () => { },
    },
}

// #### MODAL_CFGS ####
const clOptions = [
    { value: 0, text: '不涉密' },
    { value: 1, text: '秘密' },
    { value: 2, text: '保密' },
    { value: 3, text: '机密' },
    { value: 4, text: '绝密' },
]
const sizeUnit = [
    { value: 0, text: 'B' },
    { value: 1, text: 'KB' },
    { value: 2, text: 'MB' },
    { value: 3, text: 'GB' },
    { value: 4, text: 'TB' },
];
export const MODAL_CFGS = {
    // okText: '',  // 设置submit按钮文本
    // cancelText: '',  // 设置cance按钮文本
    title: '添加磁盘',
    defaultLabelCol: { span: 5 },
    defaultWrapperCol: { span: 15 },
    okAction: { type: 'manager/methodAdapter', method: 'add', payload: {} },
    onCancel: (dispatch) => dispatch({ type: 'manager/updateModalVisible', payload: false }),
    formItems: [
        {
            label: '标识', name: 'id',
            options: {
                rules: [{ required: true, message: '请输入至少两个字符的标识！', min: 2, }],
            },
        },
        {
            label: '盘名', name: 'desc',
            options: {
                rules: [{ required: true, message: '请输入至少两个字符的名称！', min: 2, }],
            },
        },
        {
            label: '权限', name: 'perm',
            options: {
                initialValue: 0,
                rules: [{ required: true }],
            },
        },
        {
            label: '最大容量', name: 'capacity',
            options: {
                initialValue: 0,
                rules: [{ required: true }],
            },
            html: <BeforeInputAfter
                afterOptions={sizeUnit}
                afterArgs={{ defaultValue: 3, style: { width: '60px' } }}
            />,
        },
        {
            label: '已用空间', name: 'used',
            options: {
                initialValue: 0,
            },
            html: <BeforeInputAfter
                afterOptions={sizeUnit}
                afterArgs={{ defaultValue: 3, style: { width: '60px' } }}
            />,
        },
        {
            label: '密级', name: 'cl',
            options: {
                initialValue: 1,
            },
            html: mySelect(clOptions),
        },
        {
            name: 'enable', wrapperCol: { xs: { span: 15 }, md: { span: 15, offset: 5 }, lg: { span: 15, offset: 5 }, },
            options: {
                valuePropName: 'checked',
                initialValue: true,
            },
            html: <Checkbox>启用</Checkbox>,
        },
    ],
}

//  #### DETAIL_ARGS ####
const leave = { type: 'manager/updateCurrentPage', payload: null }
export const DETAIL_ARGS = {
    detailTitle: '磁盘详情',
    updateTitle: '磁盘修改',
    leave: leave,
    defaultLabelCol: { span: 5 },
    defaultWrapperCol: { span: 15 },
    defaultColSpan: { xs: 24, md: 12, lg: 12 },
    content: {
        disk: [
            {
                label: '描述', index: 'desc', canUpdate: true,
            },
            {
                label: 'ID', index: 'id',
            },
            {
                label: '权限', index: 'perm', canUpdate: true,
            },
            {
                label: '创建时间', index: 'createdAt', handle: (text) => stringToDate(text),
            },
            {
                label: '最大容量', index: 'capacity', canUpdate: true, handle: (text) => sizeFormat(text),
                elem: <BeforeInputAfter 
                    afterOptions={sizeUnit}
                    afterArgs={{ style: { width: '60px' } }}
                    />
            },
            {
                label: '已用空间', index: 'used', handle: (text) => sizeFormat(text),
            },
            {
                label: '密级', index: 'cl', canUpdate: true, elem: mySelect(clOptions),
            },
            {
                label: 'pid', index: 'pid',
            },
            {
                label: '系统路径', index: 'root',
            },
            {
                label: '系统创建', index: 'isSys',
            },
            {
                label: '启用', index: 'enable', canUpdate: true, elem: <Checkbox />,
                options: {
                    valuePropName: 'checked',
                    initialValue: false,
                },
            },
            {
                label: 'vDir', index: 'vDir', canUpdate: true, elem: <Checkbox />,
                options: {
                    valuePropName: 'checked',
                    initialValue: false,
                },
            },
        ]
    },
    footer: [
        {
            elem: Icon,
            style: { float: 'left', height: '32px', lineHeight: '32px' },
            args: {
                type: 'arrow-left',
            },
            onClick: (dispatch) => { dispatch(leave) },
            // html: <xxx />  // 复杂的直接返回组件
        },
        {
            mark: 'detail',
            elem: Button,
            style: { float: 'right', marginRight: '8px' },
            args: {
                // icon: 'edit',
                type: 'danger',
            },
            text: '删除',
            onClick: (dispatch) => { }
        },
        {
            mark: 'detail',
            elem: Button,
            style: { float: 'right', marginRight: '8px' },
            args: {
                // icon: 'edit',
                type: 'primary',
            },
            text: '修改',
            onClick: (dispatch, record) => {
                dispatch({ type: 'manager/updateCurrentPage', payload: { mark: 'update', record } })
            }
        },
        {
            mark: 'update',
            elem: Button,
            style: { float: 'right', marginRight: '8px' },
            text: '取消',
            onClick: (dispatch) => { }
        },
        {
            mark: 'update',
            elem: Button,
            style: { float: 'right', marginRight: '8px' },
            args: {
                // icon: 'edit',
                type: 'primary',
                // htmlType="submit",
            },
            text: '保存',
            isValidate: true,
            onClick: (dispatch, record, updateFields) => {
                return dispatch({ type: 'manager/methodAdapter', method: 'update', payload: { value: updateFields, rows: [record] } })
            }
        },
    ],
}